package com.instpower.things.util;

import com.instpower.things.helper.Checksum;
import com.instpower.things.instpower.DeviceSession;
import com.instpower.things.instpower.DeviceSessionManager;
import com.instpower.things.transport.DataBuffer;
import com.instpower.things.transport.Ender;
import com.instpower.things.transport.Header;
import com.instpower.things.transport.Response;


import java.nio.ByteBuffer;

/**
 * @author mac
 * 机柜查询的工具类
 */
public class CabinetQueryUtil {
    /**
     * 发送机柜信息查询命令
     *
     * @param cabinetSn
     */
    public static void cabinetQuery(String cabinetSn) {
        //组建08F6指令
        Response data08F6 = new Response();
        Header header08F6 = new Header();
        header08F6.setPtrol((byte) 0x02);
        header08F6.setAddr(cabinetSn);
        header08F6.setCommand((short) 0x08F6);
        header08F6.setLength((short) (2));
        data08F6.setHeader(header08F6);
        DataBuffer buffer08F6 = data08F6.encode();
        int checksum08F6 = Checksum.crc16(Checksum.CRC16_ANKER,
                ByteBuffer.wrap(buffer08F6.readByteArray(buffer08F6.readableBytes())));
        //do release after read
        buffer08F6.release();
        Ender ender08F6 = new Ender();
        ender08F6.setChecksum(checksum08F6);
        data08F6.setEnder(ender08F6);
        DeviceSession deviceSession = DeviceSessionManager.getInstance().get(cabinetSn);
        if(null !=deviceSession){
            deviceSession.getMConnection().sendResponse(data08F6);
        }
    }
}
